package com.service;

import java.sql.Date;
import java.util.List;

import com.dao.TreatmentsDAO;
import com.domain.Treatments;

public class TreatmentsService {
	public int insertNewTreatment(Treatments treatment) {
		TreatmentsDAO dao = new TreatmentsDAO();
		treatment.setVersion(1);
		treatment.setModDate(new Date(new java.util.Date().getTime()));
		return dao.insert(treatment);
	}

	public List<Treatments> getAllTreatment() {
		TreatmentsDAO dao = new TreatmentsDAO();
		return dao.getAllTreatments();
	}

	public Treatments getTreatmentByCode(String code) {
		TreatmentsDAO dao = new TreatmentsDAO();
		return dao.getTreatmentByCode(code);
	}

	public int deleteTreatmentById(String code) {
		TreatmentsDAO dao = new TreatmentsDAO();
		Treatments product = getTreatmentByCode(code);
		if (product != null) {
			dao.deleteByTreatmentCode(code);
			return 1;
		} else {
			return 0;
		}
	}

	public int updateTreatmentPrice(String treatmentCode, float newSalesPrice) {
		TreatmentsDAO dao = new TreatmentsDAO();
		Treatments treatment = dao.getTreatmentByCode(treatmentCode);
		Treatments updateTreatment = new Treatments();
		updateTreatment.setTreatmentCode(treatment.getTreatmentCode());
		updateTreatment.setTreatmentName(treatment.getTreatmentName());
		updateTreatment.setSalesPrice(newSalesPrice);
		updateTreatment.setDescription(treatment.getDescription());
		updateTreatment.setModDate(new Date(new java.util.Date().getTime()));
		updateTreatment.setVersion(treatment.getVersion() + 1);
		return dao.update(updateTreatment);
	}
}
